#include <graphics.h>
#include <stdio.h>
typedef struct
{
    int i;
    int j;
}position;

int screen[10][10],compared_screen[10][10],flag=0,map[25][37];
char current_map[5];
position pos,character_pos;

void Draw_Rec(int x,int y,int color)
{
    setcolor(color);
    setfillstyle(9,color);
    rectangle(x-19,y-19,x+19,y+19);
    floodfill(x,y,color);
}

void Draw_Cir(int x,int y,int color)
{
    setcolor(color);
    setfillstyle(1,color);
    circle(x,y,19);
    floodfill(x,y,color);
}

void Draw_Character()
{
    Draw_Cir(50+40*character_pos.j,50+40*character_pos.i,YELLOW);
}

void Copy_Map()
{
    int i,j;
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
            compared_screen[i][j]=screen[i][j];
}

void Draw_Cell(int inf,int i,int j)
{
    switch(inf)
    {
        case 0:Draw_Rec(50+40*j,50+i*40,BLACK);Draw_Rec(50+40*j,50+i*40,BROWN);break;
        case 1:Draw_Rec(50+40*j,50+i*40,BLACK);Draw_Rec(50+40*j,50+i*40,GREEN);break;
        case 2:Draw_Rec(50+40*j,50+i*40,BLACK);Draw_Rec(50+40*j,50+i*40,LIGHTGRAY);break;
        case 3:Draw_Rec(50+40*j,50+i*40,BLACK);Draw_Rec(50+40*j,50+i*40,RED);break;
    }
}

void Display_Map()
{
    int i,j;
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
            if(screen[i][j]!=compared_screen[i][j])
                switch(screen[i][j])
                {
                    case 0:Draw_Rec(50+40*j,50+i*40,BLACK);Draw_Rec(50+40*j,50+i*40,BROWN);break;
                    case 1:Draw_Rec(50+40*j,50+i*40,BLACK);Draw_Rec(50+40*j,50+i*40,GREEN);break;
                    case 2:Draw_Rec(50+40*j,50+i*40,BLACK);Draw_Rec(50+40*j,50+i*40,LIGHTGRAY);break;
                    case 3:Draw_Rec(50+40*j,50+i*40,BLACK);Draw_Rec(50+40*j,50+i*40,RED);break;
                }
    Copy_Map();
}

void Read_Map(char *name)
{
    FILE *fp;
    if((fp=fopen(name,"rb"))==NULL) exit(0);
    fread(map,925,sizeof(int),fp);
    fclose(fp);
}

void First_Used()
{
    FILE *fp;
    int i,j;
    Read_Map("map1");
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
            screen[i][j]=map[i][j];
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
            compared_screen[i][j]=-1;
    character_pos.i=1;
    character_pos.j=1;
    pos.i=0;
    pos.j=0;
    strcpy(current_map,"map1");
    setfillstyle(1,BLACK);
    floodfill(460,50,WHITE);
    setcolor(GREEN);
    settextstyle(1,0,2);
    outtextxy(460,50,"Dark");
    outtextxy(460,80,"Forest");
}

void Load_Map()
{
    int i,j,h,k;
    for(i=0,h=pos.i;h<10+pos.i;i++,h++)
        for(j=0,k=pos.j;k<10+pos.j;j++,k++)
            screen[i][j]=map[h][k];
}

void Change_Map(char *name)
{
    int i,j;   
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
            compared_screen[i][j]=-1;
    cleardevice();
    Read_Map(name);
    Load_Map();
    Display_Map();
    Draw_Character();
}

char Player_Control()
{
    char input;
    again:
    input=getch();
    switch(input)
    {
        case 'w':if (character_pos.i-1>=1&&screen[character_pos.i-1][character_pos.j]==1)
                 {
                     Draw_Cell(screen[character_pos.i][character_pos.j],character_pos.i,character_pos.j);
                     character_pos.i--;
                     Draw_Character();
                 }     
                 else if (pos.i-1>=0&&character_pos.i==1&&screen[character_pos.i-1][character_pos.j]==1) 
                 {
                     pos.i--;
                     Load_Map();
                     Display_Map();
                 }
                 break;
        case 's':if (character_pos.i+1<=8&&screen[character_pos.i+1][character_pos.j]==1)
                 {
                     Draw_Cell(screen[character_pos.i][character_pos.j],character_pos.i,character_pos.j);
                     character_pos.i++;
                     Draw_Character();
                 }    
                 else if (pos.i+1<=15&&character_pos.i==8&&screen[character_pos.i+1][character_pos.j]==1)
                 {
                     pos.i++;
                     Load_Map();
                     Display_Map();
                 }
                 break;
        case 'a':if (character_pos.j-1>=1&&screen[character_pos.i][character_pos.j-1]==1)
                 {
                     Draw_Cell(screen[character_pos.i][character_pos.j],character_pos.i,character_pos.j);
                     character_pos.j--;
                     Draw_Character();
                 }    
                 else if (pos.j-1>=0&&character_pos.j==1&&screen[character_pos.i][character_pos.j-1]==1) 
                 {
                     pos.j--;
                     Load_Map();
                     Display_Map();
                 }    
                 break;
        case 'd':if (character_pos.j+1<=8&&screen[character_pos.i][character_pos.j+1]==1)
                 {
                     Draw_Cell(screen[character_pos.i][character_pos.j],character_pos.i,character_pos.j);
                     character_pos.j++;
                     Draw_Character();
                 }    
                 else if (pos.j+1<=27&&character_pos.j==8&&screen[character_pos.i][character_pos.j+1]==1)
                 {
                     pos.j++;
                     Load_Map();
                     Display_Map();
                 }
                 break;
        case 27 :exit(0);
        default :goto again;
    }
    return input;
}

void Draw_Frame()
{
    setcolor(WHITE);
    line(30,30,430,30);
    line(430,30,430,430);
    line(430,430,30,430);
    line(30,430,30,30);
    line(450,30,600,30);
    line(450,30,450,210);
    line(600,30,600,210);
    line(450,210,600,210);
}

void Event(char ctrl)
{
    if (pos.i==0&&pos.j==27&&character_pos.i==1&&character_pos.j==8&&(strcmp(current_map,"map1")==0)&&ctrl=='d')
    {
        character_pos.i=1;
        character_pos.j=1;
        pos.i=0;
        pos.j=0;
        Change_Map("map2");
        strcpy(current_map,"map2");
        Draw_Frame();
        setfillstyle(1,BLACK);
        floodfill(460,50,WHITE);
        setcolor(RED);
        settextstyle(1,0,2);
        outtextxy(460,50,"Dead");
        outtextxy(460,80,"Mountain");
    }
    else if (pos.i==0&&pos.j==0&&character_pos.i==1&&character_pos.j==1&&(strcmp(current_map,"map2")==0)&&ctrl=='a')
    {
        character_pos.i=1;
        character_pos.j=8;
        pos.i=0;
        pos.j=27;
        Change_Map("map1");
        strcpy(current_map,"map1");
        Draw_Frame();
        setfillstyle(1,BLACK);
        floodfill(460,50,WHITE);
        settextstyle(1,0,2);
        setcolor(GREEN);
        outtextxy(460,50,"Dark");
        outtextxy(460,80,"Forest");
    }
    else if (pos.i==15&&pos.j==27&&character_pos.i==8&&character_pos.j==8&&(strcmp(current_map,"map2")==0)&&ctrl=='s')
    {
        Draw_Frame();
        setfillstyle(1,BLACK);
        floodfill(460,50,WHITE);
        settextstyle(1,0,2);
        setcolor(BLUE);
        outtextxy(460,50,"You");
        outtextxy(460,80,"Win");
        getch();
        exit(0);
    }    
}     

void Play()
{
    char ctrl;
    while(1)
    {
        ctrl=Player_Control();
        Event(ctrl);
    }
}

void Initgra()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"");
}

main()
{
    Initgra();
    First_Used();
    Draw_Frame();
    Display_Map();
    Draw_Character();
    Play();
}




